package com.adms.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.adms.domain.Message;
import com.adms.domain.MessageMapper;
import com.adms.domain.Reform;
import com.adms.domain.ReformMapper;
import com.adms.domain.ReformSign;
import com.adms.domain.ReformSignExample;
import com.adms.domain.ReformSignExample.Criteria;
import com.adms.domain.ReformSignMapper;
import com.adms.domain.User;
import com.adms.util.AdmsConfig;
import com.adms.util.ShiroUtil;
import com.adms.websocket.WebSocketServer;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;

@Service
public class ReformSignService {

	@Autowired
	private ReformSignMapper reformSignMapper;
	@Autowired
	private ReformMapper reformMapper;
	@Autowired
	private MessageMapper messageMapper;
	@Autowired
	private WebSocketServer webSocketServer;

	public ReformSign getBySignId(Integer signId) {
		return reformSignMapper.selectByPrimaryKey(signId);
	}

	public Map<String,Object> getBySignIdWithReform(Integer signId) {
		return reformSignMapper.selectByPrimaryKeyWithReform(signId);
	}

	public PageInfo<ReformSign> list(ReformSign reformSign, String timeField, Date start, Date end,
			String orderByClause, Integer pageNum, Integer pageSize, Integer navigatePages) {
		
		ReformSignExample ex = constructExample(reformSign, timeField, start, end, orderByClause);
		PageHelper.startPage(pageNum, pageSize);
		List<ReformSign> reformSigns = reformSignMapper.selectByExample(ex);
		return new PageInfo<>(reformSigns, navigatePages);
	}

	private ReformSignExample constructExample(ReformSign reformSign, String timeField, Date start, Date end,
			String orderByClause) {
		ReformSignExample ex = new ReformSignExample();
		Criteria or = ex.or();
		if(reformSign != null) {
			Integer reformId = reformSign.getReformId();
			Integer envAuditState = reformSign.getEnvAuditState();
			if(reformId != null) {
				or.andReformIdEqualTo(reformId);
			}
			if(envAuditState != null) {
				or.andEnvAuditStateEqualTo(envAuditState);
			}
		}
		
		if(StringUtil.isNotEmpty(timeField)) {
			if(timeField.equals("createTime")) {
				if(start != null) {
					if(end != null) {
						or.andCreateTimeBetween(start, end);
					}else {
						or.andCreateTimeGreaterThanOrEqualTo(start);
					}
				}else {
					if(end != null) {
						or.andCreateTimeLessThanOrEqualTo(end);
					}
				}
			}
			if(timeField.equals("envAuditTime")) {
				if(start != null) {
					if(end != null) {
						or.andEnvAuditTimeBetween(start, end);
					}else {
						or.andEnvAuditTimeGreaterThanOrEqualTo(start);
					}
				}else {
					if(end != null) {
						or.andEnvAuditTimeLessThanOrEqualTo(end);
					}
				}
			}
		}
		
		if(StringUtil.isNotEmpty(orderByClause)) {
			ex.setOrderByClause(orderByClause);
		}else {
			ex.setOrderByClause("reform_sign.create_time desc");
		}
		return ex;
	}
	
	public boolean audit(Integer signId,Integer reformId,Integer auditState, String auditView) {
		User loginUser = ShiroUtil.getUser();
		String loginRole = loginUser.getRole();
		if(loginRole.equals(AdmsConfig.ROLE_ENVDEPARTMENT) || loginRole.equals(AdmsConfig.ROLE_ENVLEADER)) {
			envAudit(signId, reformId, auditState, auditView);
		}
		return true;
	}

	private void envAudit(Integer signId,Integer reformId,Integer auditState, String auditView) {
		//获取并更新延期单
		ReformSign reformSign = reformSignMapper.selectByPrimaryKey(signId);
		reformSign.setEnvAuditState(auditState);
		reformSign.setEnvAuditView(auditView);
		reformSign.setEnvAuditTime(new Date());
		reformSignMapper.updateByPrimaryKey(reformSign);
		//获取整改单
		Reform reform = reformMapper.selectByPrimaryKey(reformId);
		//新增站内信
		Integer handleUserId = reform.getHandleUserId();
		Message message = new Message();
		message.setUserId(handleUserId);
		message.setReformId(reformId);
		message.setSignId(signId);
		message.setTitle(reform.getTitle());
		//审核通过
		if(auditState == 1) {
			message.setType(AdmsConfig.MESSAGE_TYPE_CONFIRMREFORM_PASS_BYENV);
			reform.setCanConfirm(false);
			reform.setState(AdmsConfig.REFORM_STATE_ISFINISHED);
			reform.setFinishTime(new Date());
		}
		if(auditState == 2) {
			message.setType(AdmsConfig.MESSAGE_TYPE_CONFIRMREFORM_FAIL_BYENV);
			reform.setCanConfirm(true);
		}
		messageMapper.insertSelective(message);
		webSocketServer.sendMessage(handleUserId, message);
		//更新整改单
		reformMapper.updateByPrimaryKey(reform);
	} 
}
